/**
 * jims
 */
package com.jims.master.modules.operation.service;

import java.util.List;

import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.service.ClinicOrdersCostService;
import com.jims.master.modules.doctor.service.ClinicOrdersService;
import com.jims.master.modules.operation.entity.*;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.utils.OperationStaffUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.operation.dao.OperationApplyDao;

/**
 * 手术申请Service
 *
 * @author DT
 * @version 2017-04-11
 */
@Service
@Transactional(readOnly = true)
public class OperationApplyService extends CrudService<OperationApplyDao, OperationApply> {
    @Autowired
    private OperationApplyDao operationApplyDao;
    @Autowired
    private ClinicOrdersService clinicOrdersService;
    @Autowired
    private ClinicOrdersCostService clinicOrdersCostService;
    @Autowired
    private OperationNameApplyService operationNameApplyService;
    @Autowired
    private OperationStaffApplyService operationStaffApplyService;
    @Autowired
    private OperationRoomService operationRoomService;
    @Autowired
    private  OperationDictService operationDictService;
    @Autowired
    private OrgDeptService orgDeptService;
    public Page<OperationApply> findPage(Page<OperationApply> page, OperationApply operationApply) {
        operationApply.setOperStatus("0");
        return super.findPage(page, operationApply);
    }

    /**
     * 手术名称自动补全
     * @return
     */
    public List<OperationDict> findAutoCompletePriceist(OperationDict operationDict, String orgId, String q, String limit) {
        return dao.findAutoCompletePriceList(operationDict, orgId, q, limit);
    }

    /**
     * 手术预约人员获取
     * @param operationStaffApply
     * @param state
     * @return
     */
    @Transactional(readOnly = false)
    public OperationStaffApply getStaff(OperationStaffApply operationStaffApply,String state){
        operationStaffApply.setOperationLocal(state);
        operationStaffApply=operationStaffApplyService.findIds(operationStaffApply);
        if(StringUtils.isNotBlank(operationStaffApply.getUserIds())){
            operationStaffApply=OperationStaffUtils.getStaffApplyNames(operationStaffApply);
        }
        return operationStaffApply;
    }
    /**
     * 删除
     * @param operationApply
     * @return
     */
    @Transactional(readOnly = false)
    public int deleteAll(OperationApply operationApply){
        int i=0;
        i=operationApplyDao.delete(operationApply);
        OperationNameApply operationNameApply=new OperationNameApply();
        operationNameApply.setApplyId(operationApply.getId());
        i=operationNameApplyService.delete(operationNameApply);
        OperationStaffApply operationStaffApply=new OperationStaffApply();
        operationStaffApply.setScheduleId(operationApply.getId());
        i=operationStaffApplyService.delete(operationStaffApply);
        return 1;
    }

    /**
     * 判断手术名称是否存在
     * @param operationNameApply
     * @return
     */
    @Transactional(readOnly = false)
    public int ifOperNameExist(OperationNameApply operationNameApply) {
        return operationApplyDao.ifOperNameExist(operationNameApply);
    }
    /**
     * 手术预约保存
     * @param operationApply
     * @return
     */
    @Transactional(readOnly = false)
    public int saveOperationApply(OperationApply operationApply) {
        int num=0;
        try {
        if(StringUtils.isNotBlank(operationApply.getOperatingRoomId())){
            OrgDept orgDept = new OrgDept();
            orgDept.setId(operationApply.getOperatingRoomId());
            OrgDept orgDept1=orgDeptService.get(orgDept);
            operationApply.setOperatingRoomName(orgDept1.getDeptName());
        }
        /*if(StringUtils.isNotBlank(operationApply.getOperatingRoomNo())){
            OperationRoom operationRoom = new OperationRoom();
            operationRoom.setId(operationApply.getOperatingRoomNo());
            OperationRoom operationRoom1=operationRoomService.get(operationRoom);
            operationApply.setOperatingNoName(operationRoom1.getRoomNo());
        }*/
        super.save(operationApply);
        /**
         * 保存手术项目
         */
        if (operationApply.getOperationNameApplys() != null && operationApply.getOperationNameApplys().size() > 0) {
            for (OperationNameApply operationNameApply : operationApply.getOperationNameApplys()) {
                operationNameApply.setApplyId(operationApply.getId());
                operationNameApply.setOrgId(operationApply.getOrgId());
                OperationDict operationDict=operationDictService.get(operationNameApply.getOperationId());
               //String operationName = operationDictService.getNameById(operationNameApply.getOperationId());
                operationNameApply.setOperationName(operationDict.getOperationName());
                operationNameApply.setOperationCode(operationDict.getOperationCode());
                operationNameApplyService.save(operationNameApply);
            }
        }
            //手术人员先删除再保存
            OperationStaffApply operationStaffApply=new OperationStaffApply();
            operationStaffApply.setScheduleId(operationApply.getId());
            List<OperationStaffApply> list=operationStaffApplyService.findList(operationStaffApply);
            if(list.size()>0){
                operationStaffApplyService.delStaffApply(operationApply.getId());
            }
        /**
         * 保存主刀医生
         */
        if(StringUtils.isNotBlank(operationApply.getZdStaffApply().getUserId())){
            operationApply.getZdStaffApply().setScheduleId(operationApply.getId());
            operationStaffApplyService.save(operationApply.getZdStaffApply());
        }
        /**
         * 保存麻醉医生
         */
       /* if(StringUtils.isNotBlank(operationApply.getMyStaffApply().getUserId())){
            operationApply.getMyStaffApply().setScheduleId(operationApply.getId());
            operationStaffApplyService.save(operationApply.getMyStaffApply());
        }*/
        /**
         * 保存医生助手人员
         */
        if(StringUtils.isNotBlank(operationApply.getYzStaffApply().getUserIds())){
            String userIds_z=operationApply.getYzStaffApply().getUserIds();
            String id_z=operationApply.getId();
            String local_z=operationApply.getYzStaffApply().getOperationLocal();
            OperationStaffUtils.saveStaffApply(id_z,local_z,userIds_z);
        }
        /**
         * 保存麻醉助手人员
         */
       /* if(StringUtils.isNotBlank(operationApply.getMzStaffApply().getUserIds())){
            String userIds_m=operationApply.getMzStaffApply().getUserIds();
            String id_m=operationApply.getId();
            String local_m=operationApply.getMzStaffApply().getOperationLocal();
            OperationStaffUtils.saveStaffApply(id_m,local_m,userIds_m);
        }*/
        /**
         * 保存输血医生人员
         */
        /*if(StringUtils.isNotBlank(operationApply.getSxStaffApply().getUserIds())){
            String userIds_s=operationApply.getSxStaffApply().getUserIds();
            String id_s=operationApply.getId();
            String local_s=operationApply.getSxStaffApply().getOperationLocal();
            OperationStaffUtils.saveStaffApply(id_s,local_s,userIds_s);
        }*/
            /*String ordersId=clinicOrdersService.saveOperationApplyCost(operationApply);
            clinicOrdersCostService.saveOperationApplyCostItems(operationApply,ordersId);*/
        return 1;
        }
    catch (Exception e) {
            e.getMessage();
            return 0;
        }
    }

}